<?php
    ob_start();
    session_start();
    include("dbinfo.inc.php");
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die("Unable to select database");

    // get userID from the DB using the name
    $query="select ID from Account where userName like '$_POST[userName]';";
    $resultID=mysql_query($query);
    if (!$resultID) {
        echo "Error selecting ID from Account: " . mysql_error();
        die("");
    }
    $userID=mysql_result($resultID,0, "ID");

    $trailheadquery = "SELECT * FROM TrailHead WHERE";

    // Filter starting altitude
    // get avg start alt
    $query = "SELECT avg(start_altitude) FROM Filter WHERE userID=$userID";
    $result = mysql_query($query);
    $avg_start_altitude = mysql_result($result,0,"avg(start_altitude)");
    // get number of trailhead filters w/ specified start alt
    $query = "SELECT * FROM Filter WHERE start_altitude IS NOT NULL AND 
		userID=$userID";
    $result = mysql_query($query);
    $spec_altitude = mysql_numrows($result);
    // get number of trailhead filters w/o specified start alt
    $query = "SELECT * FROM Filter WHERE start_altitude IS NULL AND 
		(dog IS NOT NULL OR bike IS NOT NULL) AND 
		userID=$userID;";
    $result = mysql_query($query);
    $any_altitude = mysql_numrows($result);

    if ($spec_altitude > $any_altitude) {
	if ($avg_start_altitude < 9000) {
	    if ($avg_start_altitude < 3000) { $altitude = 0; }
	    if ($avg_start_altitude < 6000) { $altitude = 3000; }
	    else { $altitude = 6000; }
	    $trailheadquery.=" start_altitude <= ($altitude+3000) AND
				start_altitude >= $altitude";
	}
 	else {
	    $altitude = 9000;
	    $trailheadquery.=" start_altitude >= $altitude";
	}
	$addAnd=1;
	$_SESSION['altitude'] = $altitude;
    }
    else {
	$addAnd=0;
	$_SESSION['altitude'] = NULL;
    }

    // Filter Dogs
    $query = "SELECT * FROM Filter WHERE dog='yes' AND userID=$userID;";
    $result = mysql_query($query);
    $dog_yes = mysql_numrows($result);
    $query = "SELECT * FROM Filter WHERE dog='no' AND userID=$userID;";
    $result = mysql_query($query);
    $dog_no = mysql_numrows($result);
    $query = "SELECT * FROM Filter WHERE dog IS NULL AND 
		(start_altitude IS NOT NULL OR bike IS NOT NULL) AND 
		userID=$userID;";
    $result = mysql_query($query);
    $dog_both = mysql_numrows($result);

    if ($dog_yes > $dog_no and $dog_yes > $dog_both) {
	if ($addAnd==1) { $trailheadquery.=" AND"; }
	$addAnd==1;
	$query.=" dog='yes'";
	$_SESSION['dogs'] = yes;
    }
    if ($dog_no > $dog_yes and $dog_no > $dog_both) {
	if ($addAnd==1) { $trailheadquery.=" AND"; }
	$addAnd==1;
	$query.=" dog='no'";
	$_SESSION['dogs'] = no;
    }
    else {
	$addAnd=0;
	$_SESSION['dogs'] = NULL;
    }

    // Filter Bikes
    $query = "SELECT * FROM Filter WHERE bike='yes' AND userID=$userID;";
    $result = mysql_query($query);
    $bike_yes = mysql_numrows($result);
    $query = "SELECT * FROM Filter WHERE bike='no' AND userID=$userID;";
    $result = mysql_query($query);
    $bike_no = mysql_numrows($result);
    $query = "SELECT * FROM Filter WHERE bike IS NULL AND 
		(start_altitude IS NOT NULL OR dog IS NOT NULL) AND 
		userID=$userID;";
    $result = mysql_query($query);
    $bike_both = mysql_numrows($result);

    if ($bike_yes > $bike_no and $bike_yes > $bike_both) {
	if ($addAnd==1) { $trailheadquery.=" AND"; }
	$query.=" bike='yes'";
	$_SESSION['bikes'] = yes;
    }
    if ($bike_no > $bike_yes and $bike_no > $bike_both) {
	if ($addAnd==1) { $trailheadquery.=" AND"; }
	$query.=" bike='no'";
	$_SESSION['bikes'] = no;
    }
    else {
	$_SESSION['bikes'] = NULL;
    }

    $trailheadquery.=";";
    if (strstr($trailheadquery, '=') != FALSE) {
	$_SESSION['filterTrailHeadQuery'] = $trailheadquery;
    }
    else {
	$_SESSION['filterTrailHeadQuery'] = NULL;
    }



    mysql_close();
    session_write_close();
    header("Location: index.php");
    ob_flush();
?>
